﻿function mainViewModel(serverModel) {
    var self = this;

    self.loadCategories = function (cats) {
        self.categories.removeAll();
        $.each(cats, function (index, cat) {
            self.categories.push(new category(cat.Name, cat.Subscriptions));
        });
    };

    self.categories = ko.observableArray([]);
    self.loadCategories(serverModel.Categories);
    self.categoryNames = serverModel.AllCategories;

    self.selectedCategory = ko.observable('');
    self.itemName = ko.observable('');

    self.addNewItem = function() {
        $.ajax({
            url: '/home/add',
            type: 'POST',
            data: {
                CategoryName: self.selectedCategory(),
                Name: self.itemName()
            },
            success: function (model) {
                self.loadCategories(model.Categories);
                self.itemName('');
            }
        });
    };
}
